//
//  MSTarbbarController.swift
//  weibo
//
//  Created by JLHong on 15/12/15.
//  Copyright © 2015年 long. All rights reserved.
//

import UIKit

class MSTarbbarController : UITabBarController,UITabBarControllerDelegate {
  
    //MARK:-- 属性 --
    private var customTabbar = MSCustomTabbar()
    
    var Cvc : UIViewController = (UIViewController)()
    
    //MARK:-- 生命周期 --
    override func viewDidLoad() {
        super.viewDidLoad()
   
        addChildViewController()
       self.setValue(self.customTabbar, forKeyPath: "tabBar")
       self.customTabbar.tintColor = UIColor.orangeColor()
       super.delegate = self
    }

    // MARK:  - -  添加自控制器
    private func addChildViewController () {

        // 首页
        let homeTableVC = MSHomeController()
        addChildViewController(homeTableVC, title: "首页", image:  "tabbar_home")
        
        // 消息
        let messageTableVC = MSMessageController()
        addChildViewController(messageTableVC, title: "消息", image:  "tabbar_message_center")
        
        // 发现
        let discoverTableVC = MSDiscoverController()
        addChildViewController(discoverTableVC, title: "发现", image: "tabbar_discover")
        
        // 我
        let profileTableVC = MSProfileController()
        profileTableVC.navigationController?.navigationBarHidden = true
        addChildViewController(profileTableVC, title: "我", image: "tabbar_profile")
    }
    
  private  func addChildViewController(Controller: UIViewController , title: String ,image:String)  {
        
        self.addChildViewController(UINavigationController(rootViewController: Controller))
        Controller.title = title
        let lightimage = image + "_highlighted"
        Controller.tabBarItem.image = UIImage(named: image)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        Controller.tabBarItem.selectedImage = UIImage(named: lightimage)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    }
    
    //MARK:-- 设置专场动画 --
    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {

        if  !(Cvc == viewController) {
            let animation = CABasicAnimation(keyPath: "transform.scale")
            animation.fromValue = 0.6
            animation.toValue = 1
            animation.duration = 2.0
            viewController.view.layer.addAnimation(animation, forKey: nil)
            Cvc =  viewController
        }
    }
}

